//
//  BoostDelegate.swift
//  MCFlutterModule_Example
//
//  Created by mingci on 2021/10/22.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import flutter_boost
 

class BoostDelegate: NSObject,FlutterBoostDelegate{
    
    var navigationController:UINavigationController?
    
    var resultTable:Dictionary<String,([AnyHashable:Any]?)->Void> = [:]
    
    
    func pushNativeRoute(_ pageName: String!, arguments: [AnyHashable : Any]!) {
        let isPresent = arguments["isPresent"] as? Bool ?? false
        
        let isAnimated = arguments["isAnimated"] as? Bool ?? true
        
        var targetViewController = UIViewController()
        
        if (pageName == "homePage"){
            let data:String = arguments?["data"] as? String ?? ""
            let homeVc = homeViewController()
            homeVc.dataString = data
            targetViewController = homeVc
            
        }
        
        if isPresent == true {
            self.navigationController?.present(targetViewController, animated: true, completion: {
                
            })
        } else {
            self.navigationController?.pushViewController(targetViewController, animated: true)
        }
        
    }
    
    func pushFlutterRoute(_ options: FlutterBoostRouteOptions!) {
        let vc:FBFlutterContainer = FBFlutterViewContainer()
        vc.setName(options.pageName, uniqueId: options.uniqueId, params: options.arguments, opaque: options.opaque)
        let isPresent = (options.arguments["isPresent"] as? Bool) ?? false
        
        let isAnimated = (options.arguments["isAnimated"] as? Bool) ?? true
        
        resultTable[vc.uniqueIDString()] = options.onPageFinished
        
        if isPresent || !options.opaque {
            self.navigationController?.present(vc as! UIViewController, animated: true, completion: {
                
            })
        }else {
            self.navigationController?.pushViewController(vc as! UIViewController, animated: true)
        }
        
        
        
    }
    
    func popRoute(_ options: FlutterBoostRouteOptions!) {
        if let vc = self.navigationController?.presentingViewController as? FBFlutterViewContainer,vc.uniqueIDString() == options.uniqueId {
            if vc.modalPresentationStyle == .overFullScreen {
                self.navigationController?.topViewController?.beginAppearanceTransition(true, animated: false)
                vc.dismiss(animated: true) {
                    self.navigationController?.topViewController?.endAppearanceTransition()
                }
            }else {
                vc.dismiss(animated: true, completion: nil)
            }
        }else {
            guard let viewControllers = self.navigationController?.viewControllers else { return
                
            }
            var containerToRemove:FBFlutterViewContainer?
            for item in viewControllers.reversed() {
                if let container = item as? FBFlutterViewContainer,container.uniqueIDString() == options.uniqueId {
                    containerToRemove = container
                    break
                }
            }
            if containerToRemove == nil {
                fatalError("uniqueid is wrong!!!")
            }
            
            if self.navigationController?.topViewController == containerToRemove {
                self.navigationController?.popViewController(animated: true)
            }else {
                containerToRemove?.removeFromParentViewController()
            }
            
        }
        
        if  let onPageFinshed = resultTable[options.uniqueId] {
            onPageFinshed(options.arguments)
            resultTable.removeValue(forKey:options.uniqueId)
        }
        
    }
    
    
}
